package net.sk.china.city.utils;

import net.sk.china.city.utils.ErrorEnum.IError;
import lombok.Data;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

public class R<T> extends ResponseEntity<R.Message<T>> {
    @Data
    public static class Message<T> {
        private int code;
        private String msg;
        private T data;

        public Message(int code, String msg, T data) {
            this.code = code;
            this.msg = msg;
            this.data = data;
        }
    }

    public R(Message<T> body, HttpStatus status) {
        super(body, status);
    }

    public static <T> R<T> success(T data) {
        return new R<>(new Message<>(200, "请求成功", data), HttpStatus.OK);
    }

    public static <T> R<T> fail(IError error) {
        return requestFail(HttpStatus.OK, error.getCode(), error.getMessage());
    }
    public static <T> R<T> fail(int code, String msg) {
        return requestFail(HttpStatus.OK, code, msg);
    }
    public static <T> R<T> requestFail(HttpStatus httpStatus, int code, String msg) {
        return requestFail(httpStatus, code, msg, null);
    }

    public static <T> R<T> requestFail(HttpStatus httpStatus, int code, String msg, T data) {
        return new R<>(new Message<>(code, msg, data), httpStatus);
    }

    public static <T> R<T> serverFail(int code, String msg) {
        return serverFail(code, msg, null);
    }
    public static <T> R<T> serverFail(int code, String msg, T data) {
        return requestFail(HttpStatus.INTERNAL_SERVER_ERROR, code, msg, data);
    }

    public static <T> R<T> serverFail(HttpStatus httpStatus, int code, String msg, T data) {
        return requestFail(httpStatus, code, msg, data);
    }
}
